const dayjs = require("dayjs")
const { query } = require("../util/dbutil")
const { AssertUtils, IdUtils, RandomUtils } = require("../util/utils")
const sqlWhere = require("../util/SqlWhere");
const sqlUpdate = require("../util/SqlUpdate");




const getCarList = async (query) => {
    const arr = ['id','%license_plate%','%vehicle_model%','user_id']
    const data = await new sqlWhere(`select * from car join car_info on car.id = car_info.car_id`, query)
        .addQueryCriteriaMultiple(arr)
        .setPagination()
        .executeTheQuery()
    const result = await new sqlWhere('SELECT COUNT(*) AS count FROM car',query)
        .addQueryCriteriaMultiple(arr)
        .executeTheQuery()
    return {
        data,
        result
    }
}

const addCar = async (data) => {
    const carArr = []
    const carInfoArr = []
    const carValueArr = []
    const carInfoValueArr = []
    const res1 = await query('select * from car limit 1')
    const res2 = await query('select * from car_info limit 1')
    const carKeyArr = Object.keys(res1.result[0])
    const carInfoKeyArr = Object.keys(res2.result[0])

    Object.keys(data).forEach(item=>{
        if(carKeyArr.includes(item)){
            carArr.push(item)
            carValueArr.push(data[item])
        }else if(carInfoKeyArr.includes(item)){
            carInfoArr.push(item)
            carInfoValueArr.push(data[item])
        }else{
            console.log('不存在的字段')
        }
    })
    const arr1 = carArr.slice().fill('?').join()
    const arr2 = carInfoArr.slice().fill('?').join()

    const {result} = await query(`insert into car (${carArr.join()}) values (${arr1})`,carValueArr)

    carInfoArr.push('car_id')
    carInfoValueArr.push(result.insertId)
    await query(`insert into car_info (${carInfoArr.join()}) values (${arr2},?)`,carInfoValueArr)
}
const updateCar = async (data) => {
    const carArr = []
    const carInfoArr = []
    const carValueArr = []
    const carInfoValueArr = []
    const res1 = await query('select * from car limit 1')
    const res2 = await query('select * from car_info limit 1')
    const carKeyArr = Object.keys(res1.result[0])
    const carInfoKeyArr = Object.keys(res2.result[0])

    Object.keys(data).forEach(item=>{
        if(carKeyArr.includes(item)){
            carArr.push(item)
            carValueArr.push(data[item])
        }else if(carInfoKeyArr.includes(item)){
            carInfoArr.push(item)
            carInfoValueArr.push(data[item])
        }else{
            console.log('不存在的字段')
        }
    })
    if(carArr.length){
        const placeholders = carArr.map(col => `${col}=?`).join(',');
        await query(`update car set ${placeholders} where id=?`,carValueArr.concat([data.id]))
    }
    if(carInfoArr.length){
        const placeholders = carInfoArr.map(col => `${col}=?`).join(',');
        await query(`update car_info set ${placeholders} where car_id=?`,carInfoValueArr.concat([data.id]))
    }
}

const deleteCar = async (id) => {
    return await query(`DELETE FROM car WHERE id = ?`,[id])
}


module.exports = {
    addCar,
    getCarList,
    deleteCar,
    updateCar
}
